package com.dy.nygl.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.dy.nygl.pojo.Alerts;
import com.dy.nygl.service.AlertTypeService;
import com.dy.nygl.service.AlertsService;
import com.dy.nygl.service.DeviceService;

@Controller
@RequestMapping("/alerts")
public class AlertsController {

	@Resource(name="alertsService")
	private AlertsService alertsService;
	@Resource(name="alertTypeService")
	private AlertTypeService alertTypeService;
	@Resource(name="deviceService")
	private DeviceService deviceService;
	
	/**
	 * getAlertSituation 分页显示预警情况
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getAlertSituation.action")
	public ModelAndView getAlertSituation(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView model = new ModelAndView();
		int currPage = 1;
		int rowsPage = 10;
		int totalRows=alertsService.getAlertSituationCount(); //总行数
		
		int totalPage=totalRows/rowsPage;//总页数
		totalPage=(totalRows%rowsPage)>0?totalPage+1:totalPage;
		
		if (request.getParameter("currPage")!=null) {
			try { 
				currPage=Integer.parseInt(request.getParameter("currPage"));//当前显示第几页
				if(currPage < 1) {
					currPage = 1;
				} else if(currPage > totalPage) {
					currPage = totalPage;
				}
			} catch(Exception e) { 
				currPage=1;//当前显示第几页
			}
		} else {
			currPage=1;//当前显示第几页
		}		
		
		int begin=(currPage-1)*rowsPage;//计算分页开始行
		
		List<Map<String, Object>> asList= alertsService.getAlertSituation(begin, rowsPage); 
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			for(int i=0; i<asList.size(); i++) {
				Map<String, Object> map = new LinkedHashMap <String, Object>();
				map.put("deviceName", deviceService.getDeviceById(Integer.parseInt(asList.get(i).get("device_id").toString())).getNumber());
				map.put("typeName", alertTypeService.getAlertTypeById(Integer.parseInt(asList.get(i).get("type_id").toString())).getName());
				if(Integer.parseInt(asList.get(i).get("rank").toString()) == 1) {
					map.put("rankName", "一级预警");
				} else if(Integer.parseInt(asList.get(i).get("rank").toString()) == 2) {
					map.put("rankName", "二级预警");
				} else if(Integer.parseInt(asList.get(i).get("rank").toString()) == 3) {
					map.put("rankName", "三级预警");
				}
				map.put("count", Integer.parseInt(asList.get(i).get("count").toString()));
				list.add(map);					
			}	
		} catch(Exception e) {
			list = null;
		}
		
		model.addObject("currPage", currPage);
		model.addObject("rowsPage", rowsPage);
		model.addObject("totalRows", totalRows);
		model.addObject("totalPage", totalPage);
		model.addObject("list", list);
		
		model.setViewName("alerts/alert_situation");
		return model;
	}
	
	@RequestMapping("/getAlertLog.action")
	public ModelAndView getAlertLog(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView model = new ModelAndView();
		int currPage = 1;
		int rowsPage = 10;
		int totalRows=alertsService.getAlertsCount(); //总行数
		
		int totalPage=totalRows/rowsPage;//总页数
		totalPage=(totalRows%rowsPage)>0?totalPage+1:totalPage;
		
		if (request.getParameter("currPage")!=null) {
			try { 
				currPage=Integer.parseInt(request.getParameter("currPage"));//当前显示第几页
				if(currPage < 1) {
					currPage = 1;
				} else if(currPage > totalPage) {
					currPage = totalPage;
				}
			} catch(Exception e) { 
				currPage=1;//当前显示第几页
			}
		} else {
			currPage=1;//当前显示第几页
		}		
		
		int begin=(currPage-1)*rowsPage;//计算分页开始行
		
		List<Alerts> asList= alertsService.getAlertsByPages(begin, rowsPage); 
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			for(int i=0; i<asList.size(); i++) {
				Map<String, Object> map = new LinkedHashMap <String, Object>();
				map.put("deviceNum", deviceService.getDeviceById(asList.get(i).getDeviceId()).getNumber());
				if(asList.get(i).getRank() == 1) {
					map.put("rankName", "一级预警");
				} else if(asList.get(i).getRank() == 2) {
					map.put("rankName", "二级预警");
				} else if(asList.get(i).getRank() == 3) {
					map.put("rankName", "三级预警");
				}
				map.put("time", asList.get(i).getCreatetime());
				if(asList.get(i).getStatus() == 0) {
					map.put("status","未处理");
				} else if(asList.get(i).getStatus() == 1) {
					map.put("status","已处理");
				}
				
				list.add(map);					
			}	
		} catch(Exception e) {
			list = null;
		}
		
		model.addObject("currPage", currPage);
		model.addObject("rowsPage", rowsPage);
		model.addObject("totalRows", totalRows);
		model.addObject("totalPage", totalPage);
		model.addObject("list", list);
		
		model.setViewName("alerts/alert_log");
		return model;
	}
	
	@RequestMapping("/alert_manage.action")
	public ModelAndView manageAlert(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView model = new ModelAndView();
		int currPage = 1;
		int rowsPage = 10;
		int totalRows=alertsService.getUnAlertsCount(); //总行数
		
		int totalPage=totalRows/rowsPage;//总页数
		totalPage=(totalRows%rowsPage)>0?totalPage+1:totalPage;
		
		if(totalPage != 0) {
			if (request.getParameter("currPage")!=null) {
				try { 
					currPage=Integer.parseInt(request.getParameter("currPage"));//当前显示第几页
					if(currPage < 1) {
						currPage = 1;
					} else if(currPage > totalPage) {
						currPage = totalPage;
					}
				} catch(Exception e) { 
					currPage=1;//当前显示第几页
				}
			} else {
				currPage=1;//当前显示第几页
			}		
			
			int begin=(currPage-1)*rowsPage;//计算分页开始行
			List<Alerts> asList= alertsService.getUnAlertsByPages(begin, rowsPage); 
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			try {
				for(int i=0; i<asList.size(); i++) {
					Map<String, Object> map = new LinkedHashMap <String, Object>();
					map.put("id", asList.get(i).getId());
					map.put("deviceNum", deviceService.getDeviceById(asList.get(i).getDeviceId()).getNumber());
					if(asList.get(i).getRank() == 1) {
						map.put("rankName", "一级预警");
					} else if(asList.get(i).getRank() == 2) {
						map.put("rankName", "二级预警");
					} else if(asList.get(i).getRank() == 3) {
						map.put("rankName", "三级预警");
					}
					map.put("typeName", alertTypeService.getAlertTypeById(asList.get(i).getTypeId()).getName());
					map.put("content", asList.get(i).getContent());
					map.put("time", asList.get(i).getCreatetime());
					list.add(map);					
				}	
			} catch(Exception e) {
				list = null;
			}
			
			model.addObject("currPage", currPage);
			model.addObject("rowsPage", rowsPage);
			model.addObject("totalRows", totalRows);
			model.addObject("totalPage", totalPage);
			model.addObject("list", list);
		}
		model.setViewName("alerts/alert_manage");
		return model;
	}
	
	@RequestMapping("/stopAlert")
	public ModelAndView stopAlert(HttpServletRequest request) {
		String str = request.getParameter("str");
		String[] values = str.split(",");
//		try {
			for(int i=0; i<values.length; i++) {
				alertsService.updateAlertsStatus(Integer.parseInt(values[i]));
				System.out.println(values[i]);
			} 
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		ModelAndView model = new ModelAndView();
		model.setViewName("alerts/alert_manage");
		return model;
	}
	
	@RequestMapping("/deleteAlert")
	public ModelAndView deleteAlert(HttpServletRequest request) {
		String str = request.getParameter("str");
		String[] values = str.split(",");
		try {
			for(int i=0; i<values.length; i++) {
				alertsService.deleteAlerts(Integer.parseInt(values[i]));
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		ModelAndView model = new ModelAndView();
		model.setViewName("alerts/alert_manage");
		return model;
	}
	
	@RequestMapping("/alert_view")
	public ModelAndView viewAlert(HttpServletRequest request) {
		ModelAndView model = new ModelAndView();
		int currPage = 1;
		int rowsPage = 10;
		int totalRows=alertsService.getAlertsCount(); //总行数
		
		int totalPage=totalRows/rowsPage;//总页数
		totalPage=(totalRows%rowsPage)>0?totalPage+1:totalPage;
		
		if(totalPage != 0) {
			if (request.getParameter("currPage")!=null) {
				try { 
					currPage=Integer.parseInt(request.getParameter("currPage"));//当前显示第几页
					if(currPage < 1) {
						currPage = 1;
					} else if(currPage > totalPage) {
						currPage = totalPage;
					}
				} catch(Exception e) { 
					currPage=1;//当前显示第几页
				}
			} else {
				currPage=1;//当前显示第几页
			}		
			
			int begin=(currPage-1)*rowsPage;//计算分页开始行
			List<Alerts> asList= alertsService.getAlertsByPages(begin, rowsPage); 
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			try {
				for(int i=0; i<asList.size(); i++) {
					Map<String, Object> map = new LinkedHashMap <String, Object>();
					map.put("deviceNum", deviceService.getDeviceById(asList.get(i).getDeviceId()).getNumber());
					if(asList.get(i).getRank() == 1) {
						map.put("rankName", "一级预警");
					} else if(asList.get(i).getRank() == 2) {
						map.put("rankName", "二级预警");
					} else if(asList.get(i).getRank() == 3) {
						map.put("rankName", "三级预警");
					}
					map.put("typeName", alertTypeService.getAlertTypeById(asList.get(i).getTypeId()).getName());
					map.put("content", asList.get(i).getContent());
					map.put("time", asList.get(i).getCreatetime());
					if(asList.get(i).getStatus() == 0) {
						map.put("status","未处理");
					} else if(asList.get(i).getStatus() == 1) {
						map.put("status","已处理");
					}
					list.add(map);					
				}	
			} catch(Exception e) {
				list = null;
			}
			
			model.addObject("currPage", currPage);
			model.addObject("rowsPage", rowsPage);
			model.addObject("totalRows", totalRows);
			model.addObject("totalPage", totalPage);
			model.addObject("list", list);
		}
		model.setViewName("alerts/alert_view");
		return model;
	}
}
